const tools = {
  // 正式
  baseUrl: 'https://sch.antaiyi.com',
  imgUrl: 'https://sch.antaiyi.com'
}
// 数据请求
tools.request = function (url, method, params, tk = '') {
  let that = this;
  let promis = new Promise(function (resove, reject) {
    wx.request({
      url: `${that.baseUrl}${url}`,
      method: method,
      data: params,
      header: {
        'Authorization': tk, //token
        'content-type': 'application/json; charset=utf-8',
        'Cache-Control': 'max-age=10',
        'X-ISAPI': 1
      },
      success: function (res) {
        console.log(res)
        if (res.data.status==401) {
          wx.navigateTo({
            url: '/pages/logins/login/login',
          })
          return false
        }
        resove(res);
      },
      fail: (res) => {
        wx.showToast({
          title: res.errMsg,
          icon:'none'
        })
        reject(res)
      }
    })
  })
  return promis;
};


tools.showLoading = function (params) {
  wx.showLoading({
    title: '加载中...',
    mask: true
  })
}
tools.showToast = function (title, icon) {
  if (icon == 'error' || icon == 'success' || icon == undefined) icon = 'none'
  wx.showToast({
    title: title,
    icon: icon
  })
}

tools.formatting = function (list) {
  list.map(item => {
    let date = new Date(item.created_at * 1000)
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();
    var h = date.getHours();
    var i = date.getMinutes();
    var s = date.getSeconds();
    if (m <= 9) m = '0' + m;
    if (d <= 9) d = '0' + d;
    if (h <= 9) h = '0' + h;
    if (i <= 9) i = '0' + i;
    if (s <= 9) s = '0' + s;
    let batchDate = `${y}-${m}-${d} ${h}:${i}:${s}`
    item.batchDate = batchDate
    item.show = false
  })
  return list
}

module.exports = tools;